<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="handelCloes" @open="handelOpen">
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in RoleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: right">
        <el-button type="primary" @click="clickSubmit">确定</el-button>
        <el-button @click="handelCloes">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetUserDetailById, reqAssignRoles } from '@/api/employees'
import { reqGeRolesList } from '@/api/settings'
export default {
  name: 'ShowRoleDialog',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [], // 默认选择的角色
      RoleList: [], // 所有的角色种类,
      loading: true
    }
  },
  methods: {
    // 关闭时
    handelCloes() {
      this.checkList = []
      this.$emit('update:showRoleDialog', false)
    },
    // 打开时
    handelOpen() {
      this.loading = true
      Promise.all([this.getRoles(), this.GetUserDetailById()]).then(() => {
        this.loading = false
      })
    },
    // 所有的角色种类
    async getRoles() {
      const { data: { rows }} = await reqGeRolesList(1, 100)
      this.RoleList = rows
    },
    // 给当前的行，绑定已有的角色
    async GetUserDetailById() {
      const { data: { roleIds }} = await reqGetUserDetailById(this.userId)
      this.checkList = roleIds
    },
    // 重新提交修改员工角色分配
    async clickSubmit() {
      const res = await reqAssignRoles(this.userId, this.checkList)
      this.$message.success(res.message)
      this.handelCloes()
    }
  }
}
</script>

<style>

</style>
